#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Analog_to_Digital.h"
#include "Timer.h"
#include "DMA_TRANS.h"

uint16_t adc_val=0;

static uint8_t dma_success_interrupt=0;

int main(void)
{
	/*模块初始化*/
	OLED_Init();		//OLED初始化
	
    
    //timer_init();
    
    //init_adc();
    
	/*OLED显示*/

    uint32_t source[] = {1,2,3};
	OLED_ShowString(1, 1, "source:");
    
    OLED_ShowHexNum(1, 8, (uint32_t)source,8);
    
    
    
    
    uint32_t dest[3];
    OLED_ShowString(3, 1, "dest:");
    OLED_ShowHexNum(3, 8, (uint32_t)dest,8);
	
    trans_m2m_dma((uint32_t)source,(uint32_t)dest);
    DMA_SetCurrDataCounter(DMA1_Channel5,3);
    DMA_Cmd(DMA1_Channel5,ENABLE);
    
	while (1)
	{
		OLED_ShowNum(2,1,++source[0],3);
        OLED_ShowNum(2,5,++source[1],3);
        OLED_ShowNum(2,10,++source[2],3);
        
        Delay_s(1);
        
        
        if(dma_success_interrupt){
            
            OLED_ShowNum(4,1,dest[0],3);
            OLED_ShowNum(4,5,dest[1],3);
            OLED_ShowNum(4,10,dest[2],3);
            
            OLED_ShowNum(4,16,dma_success_interrupt,1);
            
            // 如果要重新启动 DMA
            // 1、那么需要先 disable
            // 2、设置计数器
            // 3、启用DMA   enable
            DMA_Cmd(DMA1_Channel5,DISABLE);
            DMA_SetCurrDataCounter(DMA1_Channel5,3);
            DMA_Cmd(DMA1_Channel5,ENABLE);
            
            dma_success_interrupt=0;
        }
        
        
	}
}

void DMA1_Channel5_IRQHandler(void){

    if(DMA_GetITStatus(DMA1_IT_TC5)==SET){
        
        dma_success_interrupt = 1;
        DMA_ClearITPendingBit(DMA1_IT_TC5);
    }

}


void ADC1_2_IRQHandler(void){
    
    if(ADC_GetITStatus(ADC1,ADC_IT_EOC)==SET){
    
        adc_val=ADC_GetConversionValue(ADC1);
        
        ADC_ClearITPendingBit(ADC1,ADC_IT_EOC);
    }
    

}
